<?php
namespace PWC;
class Common{
    public static function xmlConfig($file){
        $dir = dirname(realpath($file));
        if(!$dir) throw new \Exception("File [$file] not exists!");

        libxml_use_internal_errors(true);
        $config = simplexml_load_file($file);
        if(!$config) throw new \Exception("XML file [$file] format error!");
        libxml_use_internal_errors(false);

        $data = new \StdClass();
        $data->cookie_store_path = isset($config->cookie_store_path) ? ((string)$config->cookie_store_path) : '/tmp';
        $data->wait_usleep_time  = isset($config->wait_usleep_time) ? ((string)$config->wait_usleep_time) : 200000;
        $data->error_log         = isset($config->error_log) ? ((string)$config->error_log) : '/tmp/pwc_error_log.txt';
        $data->connects = array();
        $data->sites    = array();

        $funcs = array();
        foreach($config->xpath('/config/processors/processor') as $index=>$processor){
            $item = array();
            $attr = $processor->attributes();
            # check file
            if(!isset($attr['file'])){
                throw new \Exception('Config: processor file not exists');
            }
            if($attr['file']{0}!='/') $attr['file']=realpath($dir.'/'.$attr['file']);
            if(!file_exists($attr['file'])){
                throw new \Exception('Config: processor file not exists');
            }
            $item['file'] = (string)$attr['file'];

            # check func
            $item['func'] = isset($attr['func']) ? (string)$attr['func'] : 'main';
            if(isset($funcs[$item['func']])){
                throw new \Exception("Config: processor func({$item['func']}) has exists");
            }
            $funcs[$item['func']] = true;

            $item['handle_status'] = isset($attr['handle_status']) ? (string)$attr['handle_status'] : '2\d\d';
            $data->processors[$index] = $item;
        }

        foreach($config->xpath('/config/connects/connect') as $index=>$connect){
            $item = array();
            foreach($connect->attributes() as $key=>$attribute){
                $item[$key] = (string)$attribute;
            }
            if(!isset($item['max_connect'])) $item['max_connect'] = 200;
            if(!isset($item['get_gap']))     $item['get_gap'] = 0;
            $data->connects[$index] = $item;
        }
        if(!$data->connects){
            $data->connects[] = array('max_connect'=>10000,'type'=>'local','get_gap'=>0,'host'=>'127.0.0.1');
        }

        foreach($config->xpath('/config/sites/site') as $index=>$site){
            $item = array();
            $item['site'] = (string)$site;
            foreach($site->attributes() as $key=>$attribute){
                $item[$key] = (string)$attribute;
            }
            if(!isset($item['max_connect'])) $item['max_connect'] = 200;
            if(!isset($item['update_gap']))  $item['update_gap'] = 0;
            if(!isset($item['get_gap']))     $item['get_gap'] = 0;
            $data->sites[$index] = $item;
        }
    
        $data->mysql = array(
            'host'=>(string)$config->mysql->host,
            'user'=>(string)$config->mysql->user,
            'pass'=>(string)$config->mysql->pass,
            'name'=>(string)$config->mysql->name
        );
        return $data;
    }
}
